from rest_framework import serializers
from apps.goods.models import SKU
from apps.orders.models import OrderInfo, OrderGoods


class SKUSerialzier(serializers.ModelSerializer):
    """
        SKU序列化器
    """

    class Meta:
        model = SKU
        fields = ('name', 'default_image')


class OrderGoodsSerialzier(serializers.ModelSerializer):
    """
        订单商品表序列化器
    """
    # 嵌套sku的数据进行返回  副表嵌套主表返回数据
    sku = SKUSerialzier()

    class Meta:
        model = OrderGoods
        fields = ('price', 'count', 'sku')


class OrdersSerialzier(serializers.ModelSerializer):
    """
            订单基本信息表序列化器
        """
    user = serializers.StringRelatedField(read_only=True)

    # 关联订单商品表返回数据，主表嵌套副表返回  related_name='skus'
    skus = OrderGoodsSerialzier(many=True)

    class Meta:
        model = OrderInfo
        fields = '__all__'